using System.Collections.Generic;
using UnityEngine;

namespace MeshCombine
{
	public static class MeshExtensions
	{
		public static void Combine(this Mesh mesh, MeshTransform[] meshes, bool merge, bool transform)
		{
			int num = 0;
			List<Vector3> list = new List<Vector3>();
			List<Vector3> list2 = new List<Vector3>();
			List<Vector4> list3 = new List<Vector4>();
			List<Vector2> list4 = new List<Vector2>();
			List<Vector2> list5 = new List<Vector2>();
			List<Vector2> list6 = new List<Vector2>();
			List<Vector2> list7 = new List<Vector2>();
			List<Color> list8 = new List<Color>();
			List<int> list9 = new List<int>();
			Debug.Log((object)("meshes: " + meshes.Length));
			foreach (MeshTransform meshTransform in meshes)
			{
				Mesh mesh2 = meshTransform.Mesh;
				int[] triangles = null;
				if (merge)
                {
                    triangles = mesh2.triangles;
                }
				Vector3[] vertextData = MeshCombineUtil.GetVertextData(mesh2.vertices, triangles);
				Vector3[] vertextData2 = MeshCombineUtil.GetVertextData(mesh2.normals, triangles);
				Vector4[] vertextData3 = MeshCombineUtil.GetVertextData(mesh2.tangents, triangles);
				Vector2[] vertextData4 = MeshCombineUtil.GetVertextData(mesh2.uv, triangles);
				Vector2[] vertextData5 = MeshCombineUtil.GetVertextData(mesh2.uv2, triangles);
				Vector2[] vertextData6 = MeshCombineUtil.GetVertextData(mesh2.uv3, triangles);
				Vector2[] vertextData7 = MeshCombineUtil.GetVertextData(mesh2.uv4, triangles);
				Color[] vertextData8 = MeshCombineUtil.GetVertextData(mesh2.colors, triangles);
				int[] triangles2 = mesh2.triangles;
				TransformVertices(vertextData, meshTransform.LocalToWorld);
				OffsetTriangles(triangles2, num);
				AddRange(list, vertextData);
				AddRange(list2, vertextData2);
				AddRange(list3, vertextData3);
				AddRange(list4, vertextData4);
				AddRange(list5, vertextData5);
				AddRange(list6, vertextData6);
				AddRange(list7, vertextData7);
				AddRange(list8, vertextData8);
				AddRange(list9, triangles2);
				num += mesh.vertexCount;
				Debug.Log(("_vertices: " + vertextData.Length));
			}

            mesh.vertices = list.ToArray();
            mesh.normals = list2.ToArray();
            mesh.tangents = list3.ToArray();
            mesh.uv = list4.ToArray();
            mesh.uv2 = list5.ToArray();
            mesh.uv3 = list6.ToArray();
            mesh.uv4 = list7.ToArray();
            mesh.colors = list8.ToArray();
            mesh.triangles = list9.ToArray();

            Debug.Log(" mesh.vertices: " + mesh.vertexCount);
			mesh.RecalculateNormals();
		}

		private static void AddRange<T>(List<T> target, IList<T> source)
		{
			if (target != null && source != null)
			{
				target.AddRange(source);
			}
		}

		private static void OffsetTriangles(IList<int> triangles, int offset)
		{
			for (int i = 0; i < triangles.Count; i++)
			{
				triangles[i] += offset;
			}
		}

		private static void TransformVertices(IList<Vector3> vertices, Matrix4x4 matrix)
		{
			for (int i = 0; i < vertices.Count; i++)
			{
				vertices[i] = ((Matrix4x4)(matrix)).MultiplyPoint(vertices[i]);
			}
		}
	}
}
